using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; 
using System;

public class posGrid : MonoBehaviour
{
    // 商店面板
    public GameObject storePanel;
    // 这个格子的兵种类型
    public int soldierType;
    public GameObject UdpPlatform;
    public float coolDownTime;
    public float coolDownTimeMax;
    public String[] mgTrooperName;
    // 冷却显示
    public Image darkPanel;
    public Text posMgTrooperName;
    
    void Start()
    {
        GetComponent<Button>().onClick.AddListener(TriggerMethod);
        UdpPlatform=GameObject.Find("UdpServer");
    }

    void Update()
    {
        if(coolDownTime>0)
        {
            coolDownTime-=Time.deltaTime;
            darkPanel.fillAmount =coolDownTime/coolDownTimeMax;
        }
        else if(soldierType!=0)
        {
            UdpPlatform.GetComponent<WebSocketDemo>().playerBag[int.Parse(gameObject.name)]=soldierType;
            soldierType=0;
        }
        else if(UdpPlatform.GetComponent<WebSocketDemo>().playerBag[int.Parse(gameObject.name)]==0)
        {
            darkPanel.fillAmount=1f;
        }

        //UI维护
        
        posMgTrooperName.text=mgTrooperName[UdpPlatform.GetComponent<WebSocketDemo>().playerBag[int.Parse(gameObject.name)]];
    }

    // Update is called once per frame
    void TriggerMethod()
    {
        // 激活面板
        storePanel.SetActive(true);

        // 传递参数
        storePanel.GetComponent<storeParam>().posGirdId=int.Parse(gameObject.name);
        storePanel.GetComponent<storeParam>().posGrid=gameObject.GetComponent<posGrid>();
        
    }
}
